﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data;

namespace DBManager.Admin
{
    public class Comment
    {
        public static DataTable GetAllComment(string search, string typeCM, string status)
        {
            //hien thi comment
            string dkSearch = "";
            string dkType = "";
            string dkStatus = "";
            if (search != "")
            {
                dkSearch = " and (Content like '%" + search + "%' or ObjectTitle like '%" + search + "%') ";
            }
            dkType = " and type =" + comm.to_sqltext(typeCM) + " ";
            if (status == "-1")
            {
                //k lam gi
            }
            else
            {
                //cac gia tri tuong ung
                dkStatus = " and status=" + comm.to_sqlnumber(status) + " ";
            }
            string sql = @"select content,id,ObjectTitle,dateadded,name,email,reply,status,
                        case status when 1 then N'Đã duyệt' 
                        when 0 then N'Chưa duyệt' 
                        when 2 then N'Khóa lại' end as trangthai 
                        from comments where 1=1 " + dkStatus + dkType + dkSearch + " order by status,id desc";
            return DBUtil.SelectTable(sql);
        }
        public static DataTable GetInfomationByID(string id)
        {
            string sql = "select * from comments where id="+comm.to_sqlnumber(id);
            return DBUtil.SelectTable(sql);
        }
        public static int DeleteComment(string id)
        {
            string sql = "delete from comments where id="+comm.to_sqlnumber(id);
            try
            {
                DBUtil.Execute(sql);
                return 1;
            }
            catch (Exception)
            {
                return 0;
            }
        }
        public static int StatusComment(string id,string status)
        {
            string sql = "update comments set status="+comm.to_sqlnumber(status)+" where id=" + comm.to_sqlnumber(id);
            try
            {
                DBUtil.Execute(sql);
                return 1;
            }
            catch (Exception)
            {
                return 0;
            }
        }
        public static int EditConmentComment(string id, string content,string reply)
        {
            string sql = "update comments set content=" + comm.to_sqltext(content) + ",reply=" + comm.to_sqltext(reply) + " where id=" + comm.to_sqlnumber(id);
            try
            {
                DBUtil.Execute(sql);
                return 1;
            }
            catch (Exception)
            {
                return 0;
            }
        }
    }
}
